import { Middleware,IMiddleware} from "@midwayjs/core";
import { Context,NextFunction } from "@midwayjs/koa";
@Middleware()
export class ApiMiddleware implements IMiddleware<Context, NextFunction> {
    public static getName(): string {
        return "api";
    }
    resolve() {
        return async (ctx: Context, next: NextFunction) => {
            const result = await next();
            
            // 检查控制器返回的结果是否包含错误状态码
            if (result && typeof result === 'object' && result.code && result.code >= 400) {
                // 如果是错误响应，直接返回原始响应，不进行包装
                return result;
            }
            
            // 只对成功响应进行统一包装
            return{
                code:200,
                message:"success",
                data:result,
            }
        }
    }
}